package me.firstapp.api.system;

import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

import com.wordnik.swagger.annotations.Api;
import com.wordnik.swagger.annotations.ApiOperation;

import me.firstapp.api.base.BaseApi;
import me.firstapp.common.exception.ServiceException;
import me.firstapp.common.exception.StatusHouse;
import me.firstapp.common.json.ListObject;
import me.firstapp.common.json.SingleObject;
import me.firstapp.common.utils.ResponseUtils;
import me.firstapp.module.bbs.Topic;
import me.firstapp.module.shop.Order;
import me.firstapp.module.system.Statistics;
import me.firstapp.service.bbs.CollectionService;
import me.firstapp.service.bbs.ReplyService;
import me.firstapp.service.bbs.SectionService;
import me.firstapp.service.bbs.TopicService;
import me.firstapp.service.shop.OrderService;
import me.firstapp.service.system.StatisticsService;

@Controller
@RequestMapping(value = "/batch")
@Api(value = "/batch", description = "批处理相关接口")
public class BatchApi extends BaseApi {

	@Autowired
	private OrderService orderService;

	@Autowired
	private SectionService sectionService;

	@Autowired
	private ReplyService replyService;

	@Autowired
	private TopicService topicService;

	@Autowired
	private CollectionService collectionService;

	@Autowired
	private StatisticsService statisticsService;

	@RequestMapping(value = "findOrders.do", method = RequestMethod.GET)
	@ApiOperation(value = "查询订单列表", notes = "查询订单列表")
	public void findOrders(Long memberId, Integer status, Long apiClientId, HttpServletRequest request,
			HttpServletResponse response) {
		ListObject<Order> result = new ListObject<>();
		try {
			List<Order> orders = orderService.findOrders(memberId, status, apiClientId);
			result.setStatusObject(StatusHouse.COMMON_STATUS_OK);
			result.setItems(orders);
		} catch (ServiceException e) {
			result.setStatusObject(e.getStatusObject());
		} catch (Exception e) {
			e.printStackTrace();
			result.setStatusObject(StatusHouse.COMMON_STATUS_ERROR);
		}
		ResponseUtils.renderJson(response, result);
	}

	@RequestMapping(value = "/cancelOrder.do", method = RequestMethod.POST)
	@ApiOperation(value = "取消訂單", notes = "取消訂單")
	public void cancelOrder(String orderNo, Long apiClientId, HttpServletRequest request,
			HttpServletResponse response) {
		SingleObject<Order> result = new SingleObject<>();
		try {
			Order order = orderService.executeCancelOrder(orderNo, apiClientId);
			result.setStatusObject(StatusHouse.COMMON_STATUS_OK);
			result.setObject(order);
		} catch (ServiceException e) {
			result.setStatusObject(e.getStatusObject());
		} catch (Exception e) {
			e.printStackTrace();
			result.setStatusObject(StatusHouse.COMMON_STATUS_ERROR);
		}
		ResponseUtils.renderJson(response, result);
	}

	@RequestMapping(value = "updateTopicAndReplyCounts.do", method = RequestMethod.POST)
	@ApiOperation(value = "批量更新板块话题数量和回复数量", notes = "批量更新板块话题数量和回复数量")
	public void updateTopicAndReplyCounts(Long apiClientId, HttpServletRequest request, HttpServletResponse response) {
		SingleObject<Object> result = new SingleObject<>();
		try {
			sectionService.updateTopicAndReplyCounts(apiClientId);
			result.setStatusObject(StatusHouse.COMMON_STATUS_OK);
		} catch (ServiceException e) {
			result.setStatusObject(e.getStatusObject());
		} catch (Exception e) {
			e.printStackTrace();
			result.setStatusObject(StatusHouse.COMMON_STATUS_ERROR);
		}
		ResponseUtils.renderJson(response, result);
	}

	@RequestMapping(value = "findTopicReplyCounts.do", method = RequestMethod.GET)
	@ApiOperation(value = "获取帖子回复数量", notes = "获取帖子回复数量")
	public void findTopicReplyCounts(Long topicId, Long apiClientId, HttpServletRequest request,
			HttpServletResponse response) {
		SingleObject<Object> resultJsonObject = new SingleObject<Object>();
		try {
			Long counts = replyService.findTopicReplyCounts(topicId, apiClientId);
			resultJsonObject.setObject(counts);
			resultJsonObject.setStatusObject(StatusHouse.COMMON_STATUS_OK);
		} catch (ServiceException e) {
			resultJsonObject.setStatusObject(e.getStatusObject());
		} catch (Exception e) {
			e.printStackTrace();
			resultJsonObject.setStatusObject(StatusHouse.COMMON_STATUS_ERROR);
		}
		ResponseUtils.renderJson(response, resultJsonObject);
	}

	@RequestMapping(value = "findCollectionCounts.do", method = RequestMethod.GET)
	@ApiOperation(value = "获取收藏量", notes = "获取收藏量")
	public void executeFindCollectionCounts(Long memberId, Long topicId, Long productId, Long apiClientId,
			HttpServletRequest request, HttpServletResponse response) {
		SingleObject<Object> resultJsonObject = new SingleObject<Object>();
		try {
			Long counts = collectionService.findCollectionCounts(memberId, topicId, productId, apiClientId);
			resultJsonObject.setObject(counts);
			resultJsonObject.setStatusObject(StatusHouse.COMMON_STATUS_OK);
		} catch (ServiceException e) {
			resultJsonObject.setStatusObject(e.getStatusObject());
		} catch (Exception e) {
			e.printStackTrace();
			resultJsonObject.setStatusObject(StatusHouse.COMMON_STATUS_ERROR);
		}
		ResponseUtils.renderJson(response, resultJsonObject);
	}

	@RequestMapping(value = "/updateTopic.do", method = RequestMethod.POST)
	@ApiOperation(value = "更新話題", notes = "更新話題")
	public void updateTopic(@RequestBody Map<String, Object> body, Long topicId, Long upCounts, Long viewCounts,
			Long replyCounts, Long collectCounts, Integer recommend, Long apiClientId, HttpServletRequest request,
			HttpServletResponse response) {
		SingleObject<Object> result = new SingleObject<>();
		try {
			String summary = (String) body.get("summary");
			String content = (String) body.get("content");
			Topic topic = topicService.updateTopic(topicId, summary, content, upCounts, viewCounts, replyCounts,
					collectCounts, recommend, apiClientId);
			result.setStatusObject(StatusHouse.COMMON_STATUS_OK);
			result.setObject(topic);
		} catch (ServiceException e) {
			result.setStatusObject(e.getStatusObject());
		} catch (Exception e) {
			e.printStackTrace();
			result.setStatusObject(StatusHouse.COMMON_STATUS_ERROR);
		}
		ResponseUtils.renderJson(response, result);
	}

	@RequestMapping(value = "executeStatistics.do", method = RequestMethod.POST)
	@ApiOperation(value = "统计信息", notes = "统计信息")
	public void executeStatistics(Long apiClientId, HttpServletRequest request, HttpServletResponse response) {
		SingleObject<Statistics> result = new SingleObject<>();
		try {
			Long _apiClientId = (Long) request.getAttribute("apiClientId");
			Statistics statistics = null;
			if (_apiClientId.equals(apiClientId)) {
				statistics = statisticsService.executeStatisticsByClientId(apiClientId, true);
			} else {
				statistics = statisticsService.executeStatisticsByClientId(apiClientId, false);
			}
			result.setStatusObject(StatusHouse.COMMON_STATUS_OK);
			result.setObject(statistics);
		} catch (ServiceException e) {
			result.setStatusObject(e.getStatusObject());
		} catch (Exception e) {
			e.printStackTrace();
			result.setStatusObject(StatusHouse.COMMON_STATUS_ERROR);
		}
		ResponseUtils.renderJson(response, result);
	}

}
